package com.baosight.sczx.zm.ck.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ck.domain.ZMCKYXFAReport;

/**
 * 演习情况反馈
 * <p>Title: ServiceZMCK11.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCK11 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCK01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCK01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
         String fdYxId=null;
         String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		
		if(inInfo.getString("fdYxId")!=null)
		fdYxId = inInfo.getString("fdYxId");
		StaticTools.setQueryParameters(inInfo, "fdYxId", fdYxId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", UserSession.getInSessionProperty("orgCode"), true);//添加查询条件
		EiInfo  outInfo = super.query(inInfo, "ZMCKYXFAReport.query", new ZMCKYXFAReport());
		      
		
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ck.status", new String[] { "草拟", "上报", "关闭" }, new String[] { "草拟", "上报", "关闭" }, false));// 状态;
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum",fdMiningId,false));//班次
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		return outInfo;
	}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdYxId = null;
		if (inInfo.getString("fdYxId") != null)
			fdYxId = inInfo.getString("fdYxId");
		EiInfo out = new EiInfo();
		ZMCKYXFAReport _dom = new ZMCKYXFAReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdYxId(fdYxId);

			boolean validateIsClose = this.validateIsClose(_dom.getFdYxId());
			if (validateIsClose) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("该演习方案已经关闭，不能反馈！");
				return out;
			}
			Map<String, String> params = new HashMap<String, String>();
			String fdKeyId = _dom.getFdKeyId().trim();
			if (fdKeyId.equals(null) || fdKeyId.isEmpty()) {
				_dom.setFdDepartment(UserSession.getInSessionProperty("orgName").toString());
				boolean validate = validate(inInfo, _dom, params);
				if (validate) {
					inInfo = this.query(inInfo);
					return inInfo;
				}
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_TCCR_REPORT"));
				this.dao.insert("ZMCKYXFAReport.insert", _dom);
			} else {
				params.put("fdKeyId", fdKeyId);
				boolean validate = validate(inInfo, _dom, params);
				if (validate) {
					inInfo = this.query(inInfo);
					return inInfo;
				}
				this.dao.update("ZMCKYXFAReport.update", _dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo outInfo = new EiInfo();
		ZMCKYXFAReport _dom = new ZMCKYXFAReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdYxId=null;
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			fdYxId = _dom.getFdYxId();

			boolean validateIsClose = this.validateIsClose(fdYxId);
			if (validateIsClose) {
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				outInfo.setMsg("该演习方案已经关闭，不能删除！");
				return outInfo;
			}			
			
			this.dao.delete("ZMCKYXFAReport.delete", _dom);		
		}
		inInfo.set("fdYxId", fdYxId);
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo; 	
	}
	/**
	 * 
	 * <p>Function: validateIsReport</p>
	 * <p>Description: 校验演喜方案是否已关闭</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午9:54:29 郭淑芳
	 * </p>
	 *
	 */
	private boolean validateIsClose(String fdKeyId) {
		if (fdKeyId == null)
			return true;

		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdKeyId", fdKeyId);
		_map.put("fdStatus", "关闭");
		List result = dao.query("ZMCKYXFA.query", _map);
		if (result == null || result.isEmpty()) {

			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 每个部门每班只能有一条反馈信息</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日上午10:30:34 郭淑芳
	 * </p>
	 *
	 */
	private boolean validate(EiInfo inInfo, ZMCKYXFAReport _dom, Map<String, String> params) {
		if (params == null) {
			params = new HashMap<String, String>();
		}

		String fdYxId = _dom.getFdYxId();
		String fdDepartmentId = _dom.getFdDepartmentId();
		String fdWorknumId = _dom.getFdWorknumId();

		params.put("fdYxId", fdYxId);
		params.put("fdDepartmentId", fdDepartmentId);
		params.put("fdWorknumId", fdWorknumId);

		List result = dao.query("ZMCKYXFAReport.validate", params);
		if (result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "演习方案每部门每班只能反馈一次！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}